<template>
  <div class="app-container">
    <!-- 分页查询 -->

    <el-form
      ref="queryParams"
      :model="queryParams"
      size="small"
      label-suffix=":"
      label-width="auto"
    >
      <el-row :gutter="10">
        <el-col :span="1.5">
          <el-form-item :label="$t('goodsSKULang.skuStatus')" prop="status">
            <el-select
              v-model="queryParams.orderStatus"
              :placeholder="$t('pleaseSelect')"
              style="width: 200px"
            >
              <el-option
                v-for="item in deliveryStatusOptions"
                :key="item.value"
                :value="item.value"
                :label="$t('statusLang.' + item.title)"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="1.5">
          <el-form-item :label="$t('depotLang.depot')" prop="depotId">
            <el-select
              v-model="queryParams.depotId"
              :placeholder="$t('pleaseSelect')"
              style="width: 200px"
            >
              <el-option
                v-for="item in depotListAllStatusList"
                :key="item.value"
                :value="item.value"
                :label="$t(item.title)"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="1.5">
          <el-form-item :label="$t('harvestLang.orderNumber')" prop="orderNo">
            <el-input
              v-model="queryParams.orderNo"
              type="textarea"
              autosize
              :placeholder="$t('pleaseEnter') + $t('MultipleSpacesSeparated')"
              style="width: 260px"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="1.5">
          <el-form-item prop="customCode">
            <template>
              <el-select
                v-model="queryParams.dateType"
                @change="changeDateType"
              >
                <el-option
                  v-for="item in dateTypeOptions"
                  :key="item.value"
                  :value="item.value"
                  :label="$t(item.title)"
                />
              </el-select>
              <el-date-picker
                v-model="queryParams.dateTime"
                type="datetimerange"
                value-format="timestamp"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                @change="changeDateTime"
              />
            </template>
          </el-form-item>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            size="small"
            icon="el-icon-search"
            @click="handleQuery"
          >
            {{ $t("query") }}
          </el-button>
          <el-button
            type="primary"
            size="small"
            icon="el-icon-refresh-left"
            @click="handleReset"
          >
            {{ $t("reset") }}
          </el-button>
        </el-col>
      </el-row>
    </el-form>
    <!-- 数据列表 -->
    <el-table
      ref="table"
      v-loading="loading"
      :data="tableData"
      class="mt-10"
      size="mini"
    >
      <el-table-column
        :label="$t('tableIndex')"
        type="index"
        align="center"
        width="80"
      />
      <el-table-column
        :label="$t('goodsSKULang.baseSkuInfoDTO')"
        prop="orderNo,channelName,orderStatus"
        align="left"
        min-width="200"
      >
        <template slot-scope="scope">
          <p>
            {{ $t("harvestLang.orderNumber") + " :" }}
            <span>{{ scope.row.orderNo }}</span>
          </p>
          <p>
            {{ $t("harvestLang.logisticsProducts") + " :" }}
            <span>{{ scope.row.channelName }}</span>
          </p>
          <p>
            {{ $t("statusLang.status") + " :" }}
            <span>
              {{
                $t(
                  "statusLang." +
                    getTitleByValue(
                      deliveryStatusOptions,
                      scope.row.orderStatus
                    )
                )
              }}</span>
          </p>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('warehouseEntryLang.providerInfo')"
        prop="providerMethod,provider"
        min-width="160"
      >
        <template v-slot="{ row }">
          <p>
            {{ $t("harvestLang.providerChannel") + " :"
            }}<span>{{ row.providerChannel }}</span>
          </p>
          <p>
            {{ $t("warehouseEntryLang.provider") + " :"
            }}<span>{{ row.providerName }}</span>
          </p>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('goodsSKULang.customerCodeAndName')"
        prop="customerCodeAndName"
        align="center"
        min-width="120"
      />
      <el-table-column
        :label="$t('goodsSKULang.depotCodeAndName')"
        prop="depotCodeAndName"
        align="center"
        min-width="200"
      />
      <el-table-column
        :label="$t('goodsSKULang.boxNumber')"
        prop="boxNum"
        align="center"
        min-width="200"
      />
      <el-table-column
        :label="$t('goodsSKULang.baseSkuInfoDTO')"
        prop="skuTypeNum,skuNum"
        align="left"
        min-width="200"
      >
        <template v-slot="{ row }">
          <p>
            {{ $t("goodsSKULang.productCategory") + " :"
            }}<span>{{ row.skuTypeNum }}</span>
          </p>
          <p>
            {{ $t("goodsSKULang.productQuantity") + " :"
            }}<span>{{ row.skuNum }}</span>
          </p>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('goodsSKULang.weight')"
        prop="preWeight,actualWeight,feeWeight"
        align="left"
        min-width="200"
      >
        <template v-slot="{ row }">
          <p>
            {{ $t("unitLang.forecastWeight") + "(KG)" + " :"
            }}<span>{{ row.preWeight }}</span>
          </p>
          <p>
            {{ $t("unitLang.actualWeight") + "(KG)" + " :"
            }}<span>{{ row.actualWeight }}</span>
          </p>
          <p>
            {{ $t("unitLang.billingWeight") + "(KG)" + " :"
            }}<span>{{ row.feeWeight }}</span>
          </p>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('operation')"
        width="80"
        align="center"
        fixed="right"
      >
        <template v-slot="{ row }">
          <el-button type="text" @click="handleInfo(row)">
            {{ $t("info") }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 页码 -->
    <el-row type="flex" justify="end" align="middle" style="height: 60px">
      <el-pagination
        :total="total"
        layoout="total,prev,pager,next"
        :current-page="queryParams.pageNo"
        :page-size="queryParams.pageSize"
        @current-change="pageNoChange"
      />
    </el-row>

    <!-- 详情 -->
    <info-form
      v-model="infoParams.id"
      :visible.sync="detailDialog"
      :operation="infoParams.operation"
      @close="infoClose"
    />
  </div>
</template>

<script>
import InfoForm from './components/info-form.vue'
import { dateTypeDict, deliveryStatusDict } from '@/utils/dictData'
import { getDictOptionList, getTitleByValue } from '@/utils/dictUtils'
import { getDepotListAllStatus } from '@/api/business/depotManage/depot'
import { getOrderInfoPageList } from '@/api/WMS-order/warehouseEntry'

export default {
  components: { InfoForm },
  data() {
    return {
      loading: false,
      total: 0,
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        orderStatus: null,
        depotId: null,
        orderNo: null,
        dateType: 0, // 默认选择下单时间
        gmtArrivedEnd: null, // 收货时间
        gmtArrivedStart: null,
        gmtCreatedEnd: null, // 下单时间
        gmtCreatedStart: null,
        gmtExpectedEnd: null, // 预计到达
        gmtExpectedStart: null,
        gmtShelveEnd: null,
        gmtShelveStart: null // 上架
      },
      tableData: [],
      infoParams: {
        id: null,
        operation: null
      },
      detailDialog: false,
      depotListAllStatusList: [],
      dateTypeDict: dateTypeDict,
      deliveryStatusDict: deliveryStatusDict,
      getTitleByValue: getTitleByValue,
      dateTypeOptions: getDictOptionList(dateTypeDict), // 时间下拉
      deliveryStatusOptions: getDictOptionList(deliveryStatusDict) // 状态下拉
    }
  },
  created() {
    this.getDepotListAllStatus()
    this.getOrderInfoPageList()
  },
  methods: {
    // 处理时间戳
    handleTimeStamp(dateTime) {
      if (dateTime) {
        const timestamp = dateTime.map((item) => item / 1000)
        console.log(timestamp) // 时间选择器
        // 下单时间
        if (this.queryParams.dateType === 0) {
          this.queryParams.gmtCreatedStart = timestamp[0]
          this.queryParams.gmtCreatedEnd = timestamp[1]
        } else if (this.queryParams.dateType === 1) {
          this.queryParams.gmtArrivedStart = timestamp[0]
          this.queryParams.gmtArrivedEnd = timestamp[1]
        } else if (this.queryParams.dateType === 2) {
          this.queryParams.gmtArrivedStart = timestamp[0]
          this.queryParams.gmtArrivedEnd = timestamp[1]
        } else {
          this.queryParams.gmtShelveStart = timestamp[0]
          this.queryParams.gmtShelveEnd = timestamp[1]
        }
      }
    },
    changeDateType(value) {
      this.handleReset()
      console.log(value)
      this.queryParams.dateType = value
    },
    changeDateTime(dateTime) {
      this.handleTimeStamp(dateTime)
      console.log(this.queryParams.dateTime)
    },
    // 所有仓库状态下拉列表
    async getDepotListAllStatus() {
      const { data } = await getDepotListAllStatus()
      this.depotListAllStatusList = data
    },
    // 获取入库单列表
    async getOrderInfoPageList() {
      const { data } = await getOrderInfoPageList({
        ...this.queryParams
      })
      console.log(data, '入库单列表')
      this.tableData = data.orderInfoPageDTOList
      this.total = data.totalRowNum
    },

    // 查询
    async handleQuery() {
      this.queryParams.pageNo = 1
      this.getOrderInfoPageList()
    },
    // 重置
    async handleReset() {
      // await this.$refs.queryParams.resetFields();
      this.queryParams = {
        pageNo: 1,
        pageSize: 10,
        orderStatus: null,
        depotId: null,
        orderNo: null,
        dateType: null, // 默认选择下单时间
        gmtArrivedEnd: null, // 收货时间
        gmtArrivedStart: null,
        gmtCreatedEnd: null, // 下单时间
        gmtCreatedStart: null,
        gmtExpectedEnd: null, // 预计到达
        gmtExpectedStart: null,
        gmtShelveEnd: null,
        gmtShelveStart: null // 上架
      }
    },
    // 详情
    async handleInfo(row) {
      console.log(row)
      this.infoParams.id = row.orderId
      this.infoParams.operation = 0
      this.detailDialog = true
    },
    pageNoChange(newPage) {
      this.queryParams.pageNo = newPage
      this.getOrderInfoPageList()
    },
    infoClose() {
      this.infoParams.id = null
      this.infoParams.operation = null
      this.detailDialog = false
      this.getOrderInfoPageList()
    }
  }
}
</script>

<style></style>
